package com.br.modelo.dao;

import java.text.ParseException;
import java.util.List;
import com.br.modelo.entidades.Doc;
import com.br.modelo.entidades.MovDoc;
import com.br.modelo.entidades.Status;
import com.br.modelo.entidades.TpDoc;

public class DocDao {
	private DaoResource daoResource = new DaoResource();
	private Doc doc = new Doc();
	private MovDocDao movDocDao = new MovDocDao();

	public List<Doc> entradaDocInSetor(int nrDoc, String descDest, String descDoc, String descResp, int cdStatus, 
			int cdTpDoc, String descStatus,
			int cdSetorOrigem, int cdSetorDest, String data, String formatoData, String obsMovDoc) throws ParseException {

		List<Status> status = daoResource.select("from Status where cdStatus = :id", "id", cdStatus);		
		List<TpDoc> tpDoc = daoResource.select("from TpDoc where cdTpDoc = :id", "id", cdTpDoc);		

		doc.setNrDoc(nrDoc);
		doc.setDescDest(descDest);
		doc.setDescDoc(descDoc);
		doc.setDescResp(descResp);
		doc.setDescStatus(descStatus);
		doc.setStatus(status.get(0));
		doc.setTpDoc(tpDoc.get(0));

		daoResource.save(doc);
		List<Doc> d = daoResource.select("from Doc order by cdDoc desc limit 1");
		daoResource.endConnection();
		
		movDocDao.entradaMovDocFromSetorToSetor(cdSetorOrigem, cdSetorDest, d.get(0).getCdDoc(), data, formatoData, obsMovDoc);

		return d;
	}
	
	public List<Doc> entradaDocInArquivo(int nrDoc, String descDest, String descDoc, String descResp, 
			int cdStatus, int cdTpDoc, String descStatus,
			int cdSetorOrigem, int cdPastaParticao, String data, String formatoData, String obsMovDoc) throws ParseException {

		List<Status> status = daoResource.select("from Status where cdStatus = :id", "id", cdStatus);           
		List<TpDoc> tpDoc = daoResource.select("from TpDoc where cdTpDoc = :id", "id", cdTpDoc);                

		doc.setNrDoc(nrDoc);
		doc.setDescDest(descDest);
		doc.setDescDoc(descDoc);
		doc.setDescResp(descResp);
		doc.setDescStatus(descStatus);
		doc.setStatus(status.get(0));
		doc.setTpDoc(tpDoc.get(0));

		daoResource.save(doc);          
		List<Doc> d = daoResource.select("from Doc order by cdDoc desc limit 1");

		movDocDao.entradaMovDocFromSetorToArquivo(cdSetorOrigem, cdPastaParticao, d.get(0).getCdDoc(), data, formatoData, obsMovDoc);

		return d;
	}
	
	public List<Doc> saidaDocInSetor(int cdDoc, int cdStatus, String descStatus, int cdSetorDest, String data, String formatoData, String obsMovDoc) throws ParseException {

		List<Status> status = daoResource.select("from Status where cdStatus = :id", "id", cdStatus);		
		List<Doc> dc = daoResource.select("from Doc where cdDoc = :id", "id", cdDoc);		
		List<Doc> d = daoResource.select("from Doc order by cdDoc desc limit 1");

		movDocDao.saidaMovDocFromSetorToSetor(cdSetorDest, dc.get(0).getCdDoc(), data, formatoData, obsMovDoc);

		return d;
	}
	
	public List<Doc> saidaDocInArquivo(int cdStatus, String descStatus, int cdPastaParticao, String data, String formatoData, String obsMovDoc) throws ParseException {

		List<Status> status = daoResource.select("from Status where cdStatus = :id", "id", cdStatus);           

		doc.setDescStatus(descStatus);
		doc.setStatus(status.get(0));

		daoResource.save(doc);          
		List<Doc> d = daoResource.select("from Doc order by cdDoc desc limit 1");

		movDocDao.saidaMovDocFromSetorToArquivo(cdPastaParticao, d.get(0).getCdDoc(), data, formatoData, obsMovDoc);

		return d;
	}
	
	public void movDocFromSetorToSetor(int cdDoc, int cdSetorOrigem, int cdSetorDest, String data, String formatoData, String obsMovDoc) throws ParseException {

		movDocDao.movDocFromSetorToSetor(cdSetorOrigem, cdSetorDest, cdDoc, data, formatoData, obsMovDoc);

	}

	public void movDocFromSetorToArquivo(int cdDoc, int cdSetorOrigem, int cdPastaParticao, String data, String formatoData, String obsMovDoc) throws ParseException {

		movDocDao.movDocFromSetorToArquivo(cdSetorOrigem, cdDoc, cdPastaParticao, data, formatoData, obsMovDoc);

	}

	public void movDocFromArquivoToSetor(int cdDoc, int cdSetorDest, int cdPastaParticao, String data, String formatoData, String obsMovDoc) throws ParseException {

		movDocDao.movDocFromArquivoToSetor(cdSetorDest, cdDoc, cdPastaParticao, data, formatoData, obsMovDoc);

	}
	
	public void movDocFromArquivoToArquivo(int cdDoc, int cdPastaParticaoDest, int cdPastaParticaoOrigem, String data, String formatoData, String obsMovDoc) throws ParseException {

		movDocDao.movDocFromArquivoToArquivo(cdPastaParticaoDest, cdDoc, cdPastaParticaoOrigem, data, formatoData, obsMovDoc);

	}

	
	public Doc updateDoc(int cdDoc, String descDest, String descDoc, String descResp, 
			int cdTpDoc, int nrDoc, String descStatus) {

		List<TpDoc> tpDoc = daoResource.select("from TpDoc where cdTpDoc = :id", "id", cdTpDoc);		

		List<Doc> doc = daoResource.select("from Doc where cdDoc = :id", "id", cdDoc);

		doc.get(0).setNrDoc(nrDoc);
		doc.get(0).setDescDest(descDest);
		doc.get(0).setDescDoc(descDoc);
		doc.get(0).setDescResp(descResp);
		doc.get(0).setDescStatus(descStatus);
		doc.get(0).setTpDoc(tpDoc.get(0));

		daoResource.save(doc.get(0));	

		return doc.get(0);
	}
	
	public void baixaDoc(int cdDoc) {
		List<Doc> ldoc = daoResource.select("from Doc where cdDoc = :id", "id", cdDoc);

		ldoc.get(0).setInvalido("s");
		daoResource.save(ldoc.get(0));
	}	

	public void mudaStatusAtual(int cdDoc, int cdStatus, String descStatus) {
		List<Doc> doc = daoResource.select("from Doc where cdDoc = :id", "id", cdDoc);

		List<Status> stat = daoResource.select("from Status where cdStatus = :id", "id", cdStatus);

		doc.get(0).setStatus(stat.get(0));
		doc.get(0).setDescStatus(descStatus);

		daoResource.save(doc.get(0));
	}

	public List<Doc> listaDocValido() {
		List<Doc> lDoc = daoResource.select("from Doc where invalido = null");
		return lDoc;
	}

	public List<Doc> listaDocTudo() {
		List<Doc> lDoc = daoResource.select("from Doc");
		return lDoc;
	}
	
	public int pesquisaSetorResp(int cdDoc) {
		List<MovDoc> movDoc = daoResource.select("from MovDoc where entradaDoc = 's' and cdDoc = :id", "id", cdDoc);
		daoResource.endConnection();
		
		return movDoc.get(0).getSetorOrigem().getCdSetor();		
	}
}
